package com.mjf.scala.chapter06

/**
 * 创建对象的方式
 *
 *  -Java
 *    >new
 *    >反射
 *    >工厂
 *    >克隆
 *    >反序列化
 *
 *  -Scala
 *    >new
 *      底层调用当前类的构造方法
 *    >类()
 *      底层调用的是apply方法（apply方法定义在伴生对象中）
 */
object Scala15_TestCreateObject {

  def main(args: Array[String]): Unit = {

    // 通过new方式，底层调用的是构造方法创建对象
    val std1: Student15 = new Student15()
    println(std1)

    val std2: Student15 = new Student15("lucy", 18)
    println(std2)

    // 通过伴生对象的apply方法创建对象，
    val std3: Student15 = Student15("jack", 18)
    println(std3)

  }

}

object Student15 {

  // apply方法创建对象
  def apply(): Student15 = new Student15()

  // apply方法可以重载
  def apply(name: String, age: Int): Student15 = new Student15(name, age)

}

class Student15 {

  var name: String = _
  var age: Int = _

  def this(name: String, age: Int) {
    this()
    this.name = name
    this.age = age
  }

  override def toString = s"Student15(${name}, ${age})"
}
